/*
 * (C) Copyright 2013 Aepona Limited. All Rights Reserved.
 *
 * These materials are unpublished, proprietary, confidential source code of Aepona Limited and constitute a
 * TRADE SECRET of Aepona Limited.
 *
 * Aepona Limited retains all title to and intellectual property rights in these materials.
 */

package com.intel.sam.modulePartnerIntegration.functional.factoryTestClasses;

import com.intel.sam.modulePartnerIntegration.WebApplicationTestingHelper;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

/**
 * Created by IntelliJ IDEA.
 * User: ak40260
 * Date: 5/23/13
 */

public class WebApplicationUsingFactory
    extends WebApplicationTestingHelper
{

    public static WebDriver webDriver;
    public Platform platform;
    public String browser;
    public static String url = "http://localhost:8282/loginWebapp/";
    public static String correctPassword = "password";
    public static String inCorrectPassword = "password123";
    public static String correctUserNameText = "TestUser";
    public static String successVerificationText = "Welcome " + correctUserNameText;
    public static String failVerificationText = "Username or Password Error!";


    public WebApplicationUsingFactory( Platform input_platform, String input_browser )
        throws Exception
    {
        // Defining variables
        platform = input_platform;
        browser = input_browser;
    }

    // Hub
    // java -jar selenium-server-standalone-2.32.0.jar -role hub

    // Node

    // Chrome Node
    // java -jar selenium-server-standalone-2.32.0.jar -Dwebdriver.chrome.driver=/home/ak40260/software/selenium-2.32.0/chromedriver -role webdriver -hub http://localhost:4444/grid/register -maxSession 10 -timeout 10000 -browser browserName=chrome,maxInstances=10 -port 5557

    // Internet Explorer Node
    // java -jar selenium-server-standalone-2.32.0.jar -Dwebdriver.ie.driver=/home/ak40260/software/selenium-2.32.0/IEDriverServer.exe -role webdriver -hub http://localhost:4444/grid/register -maxSession 10 -timeout 10000 -browser browserName=InternetExplorer,maxInstances=10 -port 5556

    // FireFox Node
    // java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -maxSession 10 -timeout 10000 -browser browserName=firefox,maxInstances=10 -port 5555

    @BeforeClass()
    public void beforeClass()
        throws Exception
    {
        // Initiate WebDriver for the test
        webDriver = null;

        // Create Test User
        createTestUser();
    }

    @BeforeMethod()
    public void setup()
        throws Exception
    {
        // Initiate WebDriver for the test
        webDriver = initialiseWebDriver( platform, browser );
    }

    @Test( groups = { "CrossPlatformTests" } )
    public static void testSuccessLogInTest()
        throws Exception
    {
        // Load Web Page
        webDriver.get( url );

        // Waiting till element load
        WebDriverWait waitingTillElementLoad = new WebDriverWait( webDriver, 1 );
        waitingTillElementLoad.until( ExpectedConditions.presenceOfElementLocated(
            By.xpath( "/html/body/form/table/tbody/tr[3]/td[1]/input" ) ) );

        // Insert User Name
        WebElement userNameBox = webDriver.findElement( By.name( "username" ) );
        userNameBox.clear();
        userNameBox.sendKeys( correctUserNameText );

        // Insert Password
        WebElement passwordBox = webDriver.findElement( By.name( "password" ) );
        passwordBox.clear();
        passwordBox.sendKeys( correctPassword );

        // Submit search request
        webDriver.findElement( By.name( "login" ) ).click();

        // Waiting till element load
        Thread.sleep( 2000 );

        // Verify Logging State
        String statusMessageVerificationText = webDriver.findElement( By.xpath( "/html/body" ) ).getText();
        assertEquals( statusMessageVerificationText, successVerificationText );

    }

    @Test( groups = { "CrossPlatformTests" } )
    public static void testFailLogInTest()
        throws Exception
    {
        // Load Web Page
        webDriver.get( url );

        // Waiting till element load
        WebDriverWait waitingTillElementLoad = new WebDriverWait( webDriver, 1 );
        waitingTillElementLoad.until( ExpectedConditions.presenceOfElementLocated(
            By.xpath( "/html/body/form/table/tbody/tr[3]/td[1]/input" ) ) );

        // Insert User Name
        WebElement userNameBox = webDriver.findElement( By.name( "username" ) );
        userNameBox.clear();
        userNameBox.sendKeys( correctUserNameText );

        // Insert Password
        WebElement passwordBox = webDriver.findElement( By.name( "password" ) );
        passwordBox.clear();
        passwordBox.sendKeys( inCorrectPassword );

        // Submit search request
        webDriver.findElement( By.name( "login" ) ).click();

        // Waiting till element load
        Thread.sleep( 2000 );

        // Verify Logging State
        String statusMessageVerificationText = webDriver.findElement( By.xpath( "/html/body" ) ).getText();
        assertEquals( statusMessageVerificationText, failVerificationText );
    }

    // Supporting methods

    public static void createTestUser()
        throws Exception
    {
        // Initiate WebDriver
        webDriver = initialiseWebDriver( PLATFORM_LINUX, BROWSER_FIRE_FOX );

        // Load Web Page
        webDriver.get( url + "addUser.html" );

        // Waiting till element load
        WebDriverWait waitingTillElementLoad = new WebDriverWait( webDriver, 1 );
        waitingTillElementLoad.until( ExpectedConditions.presenceOfElementLocated(
            By.xpath( "/html/body/form/table/tbody/tr[3]/td[1]/input" ) ) );

        // Insert User Name
        WebElement userNameBox = webDriver.findElement( By.name( "new_username" ) );
        userNameBox.clear();
        userNameBox.sendKeys( correctUserNameText );

        // Insert Password
        WebElement passwordBox = webDriver.findElement( By.name( "new_password" ) );
        passwordBox.clear();
        passwordBox.sendKeys( correctPassword );

        // Submit search request
        webDriver.findElement( By.name( "createnewuser" ) ).click();

        // Waiting till element load
        Thread.sleep( 2000 );
    }
}
